package mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mvc.form.Action;
import mvc.form.ActionForm;
import mvc.unti.FullBean;

/**
 * Servlet implementation class ActionServlet
 */
public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ActionServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		System.out.println(request.getParameter("sign")+"=="+request.getParameter("username"));
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		ActionForm form = FullBean.full(request);
		Action action = null;
		Map<String,String> map = ActionMapping.getMap();
		try{
			String actionname = map.get(request.getParameter("sign"));
			Class clazz = Class.forName(actionname);
			action = (Action) clazz.newInstance();
		}catch(Exception e){
			
		}
		
		String message = action.execute(form);
		PrintWriter out = response.getWriter();
		out.print(message);
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
